package mygame.hudelements;

import com.jme3.asset.AssetManager;
import com.jme3.export.Savable;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.system.AppSettings;
import mygame.PlaneBasicControl;

/**
 * Class cares for displaying text information on the screen
 * @author Aicher
 * @version 0.0.1
 */
public class HudControl extends AbstractControl implements Savable, Cloneable {
    /**
     * Text element to place on the screen
     */
    private BitmapText dispBitmapText = null;
    
    /**
     * Used Font
     */
    private BitmapFont guiFont = null;
    
    /**
     * Gui Node - here the text element will be placed
     */
    private Node guiNode = null;
    
    /**
     * Asset manager for loading the font
     */
    private AssetManager assetManager = null;
    
    /**
     * Here we get the application settings. We need this for the screen
     * size and therefore the text positioning
     */
    private AppSettings settings = null;
    
    /**
     * This will be the displayed String (only for development purposes)
     */
    private String devDisplayString = "";

    /**
     * Empty constructor following the guidelines from JMonkeyTutorial
     */
    public HudControl() {}
    
    /**
     * Constructor with all necessary values
     * @param guiFont Font from the main method
     * @param guiNode Gui Node
     * @param assetManager Asset Manager
     * @param settings Application Settings
     */
    public HudControl(BitmapFont guiFont, Node guiNode, AssetManager assetManager, AppSettings settings) {
        this.guiFont = guiFont;
        this.guiNode = guiNode;
        this.assetManager = assetManager;
        this.settings = settings;
        this.initHud();
    }
    
    /**
     * All relevant values will be initialzed
     */
    private void initHud() {
        this.guiFont = this.assetManager.loadFont("Interface/Fonts/Default.fnt");
        this.dispBitmapText = new BitmapText(this.guiFont, false);
        this.dispBitmapText.setSize(this.guiFont.getCharSet().getRenderedSize() * 0.8f);
        this.dispBitmapText.setColor(ColorRGBA.Black);
        this.dispBitmapText.setText("");
        this.dispBitmapText.setLocalTranslation(this.guiFont.getCharSet().getRenderedSize() / 4, 
                settings.getHeight(), 0);
        this.guiNode.attachChild(this.dispBitmapText);
    }
    
    /**
     * The display String will read all information from the relevant plane (mainly the
     * basic control)
     */
    public void setDevDisplayString() {
        this.devDisplayString = "Flight Info:";
        this.devDisplayString += "\nAlt: " +
                (int)((PlaneBasicControl) spatial.getControl(PlaneBasicControl.class)).heightOverGround;
        this.devDisplayString += "\nSpeed: " +
                (int)((PlaneBasicControl) spatial.getControl(PlaneBasicControl.class)).speedInZ;
        
    }
    
    /**
     * Every instrument is hooked in via this method in order to create a central
     * point for all instruments. 
     * @param newInstrument 
     */
    public void registerNewInstrument(InstrumentControl newInstrument) {
        this.spatial.addControl(newInstrument);
    }

    
    @Override
    protected void controlUpdate(float tpf) {
        this.setDevDisplayString();
        this.dispBitmapText.setText(this.devDisplayString);
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        // Not used yet
    }
    
    @Override
    public Control cloneForSpatial(Spatial theSpatial) {
        final HudControl control = new HudControl(guiFont, guiNode, assetManager, settings);
        control.setSpatial(theSpatial);
        return control;
    }
    
}
